package com.werdior.admin.www;

import com.werdior.common.controller.AbstractController;
import com.werdior.common.util.CaptchaUtil;
import com.werdior.common.util.R;
import com.werdior.shiro.entity.User;
import com.werdior.shiro.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class LoginController extends AbstractController {


    /**
     * 登陆提交页面
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, Object> login(HttpServletRequest request,String username, String password,String verify) {
        Map<String,Object> map=new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String sessionCode = (String) session.getAttribute("code");
        session.removeAttribute("code");
        if (sessionCode == null) {
            return R.error("验证码过期请重新获取");
        }
        if (!sessionCode.toLowerCase().equalsIgnoreCase(verify.toLowerCase())) {
            return R.error("验证码错误");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
            map.put("code",0);
        } catch (AuthenticationException e) {
            String simpleName = e.getClass().getSimpleName();
            if ("UnknownAccountException".equals(simpleName)) {
                map.put("msg", "用户不存在");

            } else if("IncorrectCredentialsException".equals(simpleName)){
                map.put("msg", "密码不正确");
            }
            else if ("LockedAccountException".equals(simpleName)){
                map.put("msg", "账号被锁定,请联系管理员");
            }
        }
        return map;
    }
    /**
     * 登陆页面
     * @return
     */
    @GetMapping("/login")
    public String loginForm() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            return "redirect:/index";
        }
        return "/login";
    }

    @RequestMapping(value = "/verify", method = RequestMethod.GET)
    public void verify(HttpServletRequest request, HttpServletResponse response) {
        try {
            String verifyCode = CaptchaUtil.outputImage(response.getOutputStream());
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            session.setAttribute("code",verifyCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    


}
